#include <iostream>
#include <map>

using namespace std;

class Solution {
public:
    std::map<int, int> cache;

    int get_width(int n) {
        int res = 1;
        while (n / 10 / res) {
            res *= 10;
        }
        return res;
    }

    int countDigitOne(int n) {
        if (n == 0) return 0;
        if (cache.count(n)) return cache[n];
        // cout << n;
        int width = this -> get_width(n),
            higher = n / width,
            lower = n % width,
            width_res = this -> countDigitOne(width - 1);

        int res = this -> countDigitOne(lower);
        if (higher == 1) {
            res += width_res + 1 + lower;
        } else {
            res += width_res * (higher - 2) + this->countDigitOne(2*width - 1);
        }
        cache[n] = res;
        return res;
    }
};

int main() {
    cout << Solution().countDigitOne(23) << endl;
}
